package com.shengyo.model.dao.impl;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.shengyo.model.bean.Apparel;
import com.shengyo.model.dao.IApparelDAO;

public class ApparelDAOImpl extends BaseDAOImpl implements IApparelDAO{

	@Override
	public void delete(Apparel apparel) {
		this.getHibernateTemplate().delete(apparel);
	}

	@Override
	public Apparel findByApparelUrl(String url) {
		return (Apparel) this.getHibernateTemplate().load(Apparel.class , url);
	}

	@Override
	public Apparel findByMaterial(String material) {
		List<?> list = this.getHibernateTemplate().find("from apparel a where a.material=?",new Object[]{material});
		if (list != null && !list.isEmpty())
			return (Apparel) list.get(0);
		else
			return null;
	}

	@Override
	public Apparel findByStyle(String style) {
		List<?> list = this.getHibernateTemplate().find("from apparel a where a.stype=?",new Object[]{style});
		if (list != null && !list.isEmpty())
			return (Apparel) list.get(0);
		else
			return null;
	}

	@Override
	public List<?> findAll() {
		return this.getHibernateTemplate().find("from apparel");
	}

	@Override
	public List<?> findAll(final int first, final int max) {
        return this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery("from apparel").setFirstResult(first).setMaxResults(max).list();
			}
			
		});
	}

	@Override
	public Object findById(int id) {
		return null;
	}

	@Override
	public Apparel findById(String id) {
		return (Apparel) this.getHibernateTemplate().load(Apparel.class, id);
	}

	
}
